﻿using Shared.Method;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestSystem
{
    public partial class FormThruput : Form
    {
        public FormThruput()
        {
            InitializeComponent();
        }

        //private bool offsetMode = false;

        private void FormThruput_Load(object sender, EventArgs e)
        {
            //软件版本
            string version = Application.ProductVersion;

            this.Text = "吞吐量测试软件 V" + version;

            bool offsetMode = false;
            if (AppSetting.GetValue("Mode") == "1") { offsetMode = true; }

            //初始化数据
            string localip1 = AppSetting.GetValue("LocalIp1");
            string localip2 = AppSetting.GetValue("LocalIp2");
            string localip3 = AppSetting.GetValue("LocalIp3");
            string localip4 = AppSetting.GetValue("LocalIp4");

            string switchloaclip = AppSetting.GetValue("SwitchLocalIp");
            string switchip = AppSetting.GetValue("SwitchIp");

            string stationip = AppSetting.GetValue("StationSoftIP");

            string modemIp = AppSetting.GetValue("IpAddress");
            string modemUser = AppSetting.GetValue("UserName");
            string modemPwd = AppSetting.GetValue("Password");

            if (
                string.IsNullOrEmpty(localip1)
                || string.IsNullOrEmpty(localip2)
                || string.IsNullOrEmpty(localip3)
                || string.IsNullOrEmpty(localip4)
                || string.IsNullOrEmpty(switchloaclip)
                || string.IsNullOrEmpty(switchip)
                || string.IsNullOrEmpty(stationip)
                || string.IsNullOrEmpty(modemIp)
                || string.IsNullOrEmpty(modemUser)
                || string.IsNullOrEmpty(modemPwd))
            {
                MessageBox.Show("配置文件错误，请检查IP配置");
                Close();
                return;
            }

            int localport1, localport2, localport3, localport4, switchport, switchlocalport;
            if (
                int.TryParse(AppSetting.GetValue("LocalPort1"), out localport1) == false
                || int.TryParse(AppSetting.GetValue("LocalPort2"), out localport2) == false
                || int.TryParse(AppSetting.GetValue("LocalPort3"), out localport3) == false
                || int.TryParse(AppSetting.GetValue("LocalPort4"), out localport4) == false
                || int.TryParse(AppSetting.GetValue("SwitchPort"), out switchport) == false
                || int.TryParse(AppSetting.GetValue("SwitchLocalPort"), out switchlocalport) == false
                )
            {
                MessageBox.Show("配置文件错误，请检查端口配置");
                Close();
                return;
            }

            bool sendPack = false;
            if (AppSetting.GetValue("SendPack") == "1")
            {
                sendPack = true;
            }

            double passValueLeft, passValueRight;//passValue
            int countTimes, testTimes;//统计次数，测试次数
            if (double.TryParse(AppSetting.GetValue("PassValueLeft"), out passValueLeft) == false
                || double.TryParse(AppSetting.GetValue("PassValueRigth"), out passValueRight) == false
                || int.TryParse(AppSetting.GetValue("CountTimes"), out countTimes) == false
                || int.TryParse(AppSetting.GetValue("TestTimes"), out testTimes) == false
                )
            {
                MessageBox.Show("配置文件错误，请检查测试次数和通过状态");
                Close();
                return;
            }

            //耦合板开关编号
            string leftport1 = AppSetting.GetValue("LeftPort1");
            string rightport1 = AppSetting.GetValue("RightPort1");

            string leftport2 = AppSetting.GetValue("LeftPort2");
            string rightport2 = AppSetting.GetValue("RightPort2");

            string leftport3 = AppSetting.GetValue("LeftPort3");
            string rightport3 = AppSetting.GetValue("RightPort3");

            string leftport4 = AppSetting.GetValue("LeftPort4");
            string rightport4 = AppSetting.GetValue("RightPort4");

            if (string.IsNullOrEmpty(leftport1) || string.IsNullOrEmpty(rightport1)
                || string.IsNullOrEmpty(leftport2) || string.IsNullOrEmpty(rightport2)
                || string.IsNullOrEmpty(leftport3) || string.IsNullOrEmpty(rightport3)
                || string.IsNullOrEmpty(leftport4) || string.IsNullOrEmpty(rightport4))
            {
                MessageBox.Show("配置文件错误，耦合板开关信息错误");
                Close();
                return;
            }


            double offsetLeft1=0, offsetRight1 = 0, 
                offsetLeft2 = 0, offsetRight2 = 0, 
                offsetLeft3 = 0, offsetRight3 = 0, 
                offsetLeft4 = 0, offsetRight4 = 0;
            double showpassvalue;

            if (double.TryParse(AppSetting.GetValue("ShowPassValue"), out showpassvalue) == false)
            {
                MessageBox.Show("未配置标准通过功率[ShowPassValue]");
                Close();
                return;
            }

            if (double.TryParse(AppSetting.GetValue("offsetLeft1"), out offsetLeft1) == false
                || double.TryParse(AppSetting.GetValue("offsetRight1"), out offsetRight1) == false
                || double.TryParse(AppSetting.GetValue("offsetLeft2"), out offsetLeft2) == false
                || double.TryParse(AppSetting.GetValue("offsetRight2"), out offsetRight2) == false
                || double.TryParse(AppSetting.GetValue("offsetLeft3"), out offsetLeft3) == false
                || double.TryParse(AppSetting.GetValue("offsetRight3"), out offsetRight3) == false
                || double.TryParse(AppSetting.GetValue("offsetLeft4"), out offsetLeft4) == false
                || double.TryParse(AppSetting.GetValue("offsetRight4"), out offsetRight4) == false)
            {
                offsetMode = false;
                //offsetMode = true;
            }

            if (offsetMode)
            {
                MessageBox.Show("进入校准模式!\r\n完成后，\r\n修改为普通模式并重启软件！");
                this.BackColor = Color.DarkGreen;
            }

            //初始化测试类
            try
            {
                Testing.InitTesting(switchloaclip, switchlocalport, switchip, switchport, passValueLeft, passValueRight, countTimes, testTimes);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                Close();
                return;
            }

            this.panel1.Controls.Clear();
            this.panel2.Controls.Clear();
            this.panel3.Controls.Clear();
            this.panel4.Controls.Clear();

            ThruPutTest test = new ThruPutTest(offsetMode,showpassvalue,offsetLeft1,offsetRight1, 1, stationip, localip1, localport1, modemIp, 23, sendPack, leftport1, rightport1, modemUser, modemPwd);
            test.Show();
            this.panel1.Controls.Add(test);

            test = new ThruPutTest(offsetMode, showpassvalue, offsetLeft2, offsetRight2, 2, stationip, localip2, localport2, modemIp, 23, sendPack, leftport2, rightport2, modemUser, modemPwd);
            test.Show();
            this.panel2.Controls.Add(test);

            test = new ThruPutTest(offsetMode, showpassvalue, offsetLeft3, offsetRight3, 3, stationip, localip3, localport3, modemIp, 23, sendPack, leftport3, rightport3, modemUser, modemPwd);
            test.Show();
            this.panel3.Controls.Add(test);

            test = new ThruPutTest(offsetMode, showpassvalue, offsetLeft4, offsetRight4, 4, stationip, localip4, localport4, modemIp, 23, sendPack, leftport4, rightport4, modemUser, modemPwd);
            test.Show();
            this.panel4.Controls.Add(test);
        }


        private void FormThruput_FormClosing(object sender, FormClosingEventArgs e)
        {
            Testing.Stop();
        }

    }
}
